package com.coderman.common.starter.logparam.context;

import com.coderman.common.starter.logparam.beans.InnerBusLogBean;
import org.aspectj.lang.JoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author fanchunshuai
 * @Date 2019/4/26 21
 * @Description:
 */
@Component
public class InnerBusLogicContext {
    /**
     * 扫描注解：InnerBusLog中的属性，
     * key:方法上带有InnerBusLog注解的类路径,egg:com.xx.xxImpl.xxMethod，转换为hash值作为key
     * value:与注解InnerBusLog对应的InnerBusLogBean实体
     * 适用于DSF服务内部类
     */
    private Map<Integer, InnerBusLogBean> innerBaseBusLogMap = new HashMap<>();


    public Map<Integer, InnerBusLogBean> getInnerBaseBusLogMap() {
        return innerBaseBusLogMap;
    }


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 根据上下文获取内部服务逻辑Bean
     *
     * @return
     */
    private InnerBusLogBean getInnerBusLogBean(JoinPoint joinPoint) {
        String key = getKey(joinPoint);
        logger.debug("innner BusLog Key = {}", key);
        int hashKey = key.hashCode();
        InnerBusLogBean innerBusLogBean = this.getInnerBaseBusLogMap().get(hashKey);
        if (innerBusLogBean == null) {
            return null;
        }
        return innerBusLogBean;
    }


    public Map<String, Object> getInnerContextMap(JoinPoint joinPoint) {
        Map<String, Object> map = new HashMap<>();
        String key = getKey(joinPoint);
        String tmpKey = getTmpKey(joinPoint, key);
        logger.debug("inner BusLog key = {},tmpKey = {}", key, tmpKey);
        //获取key
        InnerBusLogBean innerBusLogBean = getInnerBusLogBean(joinPoint);
        if (innerBusLogBean == null) {
            return map;
        }
        map.put("key", key);
        map.put("tmpKey", tmpKey);
        map.put("innerBusLogBean", innerBusLogBean);
        return map;
    }


    /**
     * 获取key
     *
     * @param joinPoint
     * @return
     */
    public String getKey(JoinPoint joinPoint) {
        String methodPath = joinPoint.getSignature().toString();
        String className = joinPoint.getTarget().getClass().getName();
        int index = methodPath.lastIndexOf(".");
        String key = "";
        if (index >= 0) {
            String methodStr = methodPath.substring(index);
            key = className + methodStr;
        }
        return key;
    }


    /**
     * 获取TmpKey
     *
     * @param joinPoint
     * @return
     */
    public String getTmpKey(JoinPoint joinPoint, String key) {
        String className = joinPoint.getTarget().getClass().getName();
        String tmpKey = className.substring(className.lastIndexOf(".") + 1) + "." + key.substring(key.lastIndexOf(".") + 1);
        return tmpKey;
    }


    /**
     * 获取TmpKey
     *
     * @param joinPoint
     * @return
     */
    public String getTmpKey(JoinPoint joinPoint) {
        String key = getKey(joinPoint);
        return getTmpKey(joinPoint, key);
    }
}
